#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>

#define PLUGIN_NAME                   "l4d2_infected_kill_survivor"    //这里写插件名字
#define PLUGIN_AUTHOR                 "豆瓣酱な"                         //作者
#define PLUGIN_DESCRIPTION            "infected 普通攻击秒杀玩家"   //介绍
#define PLUGIN_VERSION                "1.0"
#define PLUGIN_URL                    "https://gitee.com/sweet-and-sour-bombs"  //网站




public Plugin myinfo =
{
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_DESCRIPTION,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_URL
}

ConVar hCvar_infectedEnable;

int g_infectedEnable;




public void OnPluginStart()
{
	RegAdminCmd("sm_ms", Command_InfectedSwitch, ADMFLAG_KICK, "infected伤害指令总开关");
	RegAdminCmd("sm_ssms", Command_InfectedSwitch, ADMFLAG_KICK, "infected伤害指令开关");
	
	hCvar_infectedEnable = CreateConVar("infected_damage_enable", "1.0", "是否启用infected伤害秒杀插件", FCVAR_NOTIFY, true, 0.0, true, 1.0);
	HookConVarChange(hCvar_infectedEnable, eConvarChanged);
	
	HookEvent("player_hurt", Event_PlayerHurt, EventHookMode_Pre); //玩家受伤

}

public void OnMapStart()
{
	CvarsChanged();
}

public void eConvarChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
{
	CvarsChanged();
}

void CvarsChanged()
{
	g_infectedEnable = hCvar_infectedEnable.IntValue;
}

public Action Command_InfectedSwitch(int client, int args)
{
	if (g_infectedEnable)
	{
		hCvar_infectedEnable.IntValue = 0;
		PrintToChat(client, "\x04[提示]\x03Infected\x05秒杀插件已\x03关闭\x05.");
	}
	else
	{
		hCvar_infectedEnable.IntValue = 1;
		PrintToChat(client, "\x04[提示]\x03Infected\x05秒杀插件已\x03开启\x05.");
	}
	return Plugin_Handled;
}

/* 玩家受伤 */
void Event_PlayerHurt(Event event, const char[] name, bool dontBroadcast)
{
	int victim = GetClientOfUserId(GetEventInt(event, "userid"));
	int attackerentid = GetEventInt(event, "attackerentid");

	char classname[20];
	if(IsValidEdict(attackerentid) && g_infectedEnable == 1)
	{
		GetEdictClassname(attackerentid, classname, 20);
		if(StrEqual(classname, "infected", false))
		{
			if(IsInfected (victim))
			return;
			
			else if(IsSurvivor (victim))
			{
				PrintToChatAll("\x04[提示]\x03Infected\x05杀死了\x03%s\x05.",  GetTrueName(victim));
				ForcePlayerSuicide(victim);
			}	
		}
	}
}	

char[] GetTrueName(int client)
{
	char sName[32];
	int Bot = IsClientIdle(client);

	if(Bot != 0)
		FormatEx(sName, sizeof(sName), "闲置:%N", Bot);
	else
		GetClientName(client, sName, sizeof(sName));
	return sName;
}

int IsClientIdle(int client) 
{
	if (!HasEntProp(client, Prop_Send, "m_humanSpectatorUserID"))
		return 0;

	return GetClientOfUserId(GetEntProp(client, Prop_Send, "m_humanSpectatorUserID"));
}

bool IsSurvivor(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2;
}

bool IsInfected(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 3;
}








